package com.leo.train.member.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.util.JwtUtil;
import com.leo.train.common.util.SnowUtil;
import com.leo.train.member.domain.Member;
import com.leo.train.member.domain.MemberExample;
import com.leo.train.member.mapper.MemberMapper;
import com.leo.train.member.dto.MemberLoginDto;
import com.leo.train.member.dto.MemberSendCodeDto;
import com.leo.train.member.vo.MemberLoginVO;
import com.leo.train.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    /**
     * 登录
     * @param req
     * @return
     */
    @Override
    public MemberLoginVO login(MemberLoginDto req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        List<Member> list = getList(mobile);
        // 判断验证码是否为空
        if (code == null || mobile == null) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_NOT_EMPTY);
        }
        // 判断手机号是否已存在
        if (CollUtil.isEmpty(list)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        // 判断手机号匹配的验证码是否正确
        String redisCode = (String) redisTemplate.opsForValue().get("memberCount: " + mobile);
        log.info("redisCode: {}", redisCode);
        if (!code.equals(redisCode)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        MemberLoginVO memberLoginVO = new MemberLoginVO();
        BeanUtils.copyProperties(list.get(0), memberLoginVO);
        // 生成token
        String token = JwtUtil.createToken(list.get(0).getId(), list.get(0).getMobile());
        memberLoginVO.setToken(token);

        // 手机号匹配的验证码正确，登录成功
        // 返回token数据
        return memberLoginVO;
    }

    /**
     * 发送验证码
     * @param req
     * @return
     */
    @Override
    public String sendCode(MemberSendCodeDto req) {
        String mobile = req.getMobile();
        List<Member> list = getList(mobile);
        if (CollUtil.isEmpty(list)) {
            // 将该手机号插入用户表
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }
        // 生成4位的随机验证码
        String code = RandomUtil.randomNumbers(4);
        log.info("生成验证码：{}", code);
        return code;
    }

    private List<Member> getList(String mobile) {
        // 查询手机号是否已存在
        // 下边这两行代码可以理解为 memberMapper.selectCount(new Member(mobile));where条件
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        return list;
    }


}
